﻿using SuperMoney.Datas;
using SuperMoney.Datas.DbModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SuperMoney.Services
{
    public class ConsumeOrderService
    {
        public static readonly ConsumeOrderService Instance;

        static ConsumeOrderService()
        {
            Instance = new ConsumeOrderService();
        }

        /// <summary>
        /// 兑换订单记录的页大小，每页15条记录
        /// </summary>
        public int pageSize
        {
            get
            {
                return 15;
            }
        }

        /// <summary>
        /// 获取指定状态的页数
        /// </summary>
        /// <param name="status">指定的状态</param>
        /// <returns></returns>
        public int GetPageCount(int status)
        {
            int recordCount = ConsumeOrderDbService.Instance.Count(status);
            int pageCount = recordCount / this.pageSize;
            if (recordCount % this.pageSize > 0)
            {
                pageCount++;
            }
            return pageCount;
        }


        /// <summary>
        /// 获取未处理的订单
        /// </summary>
        /// <param name="pageIndex">当前页码</param>
        /// <returns>返回指定页的订单列表</returns>
        public List<ConsumeOrder> GetUntreatedOrders(int pageIndex)
        {
            return ConsumeOrderDbService.Instance.GetOrders(this.pageSize, pageIndex, 1);
        }

        /// <summary>
        /// 获取已经处理的订单
        /// </summary>
        /// <param name="pageIndex">当前页码</param>
        /// <returns>返回指定页码的订单列表</returns>
        public List<ConsumeOrder> GetTreatedOrders(int pageIndex)
        {
            return ConsumeOrderDbService.Instance.GetOrders(this.pageSize, pageIndex, 2);
        }

        /// <summary>
        /// 根据指定的Id获取订单详情信息
        /// </summary>
        /// <param name="orderId">订单编号</param>
        /// <returns></returns>
        public ConsumeOrder GetSingleOrder(long orderId)
        {
            return ConsumeOrderDbService.Instance.GetSingleOrder(orderId);
        }

        /// <summary>
        /// 兑换完成
        /// </summary>
        public void Exchange(long orderId)
        {
            ConsumeOrderDbService.Instance.UpdateStatus(orderId, 2);
        }
    }
}
